<template>
	<view class="lots_box">
		<Tips/>
		<view class="choose">
			<uni-segmented-control 
				:current="formData.in_out_type" 
				:values="items" 
				style-type="button"
				:active-color="primary_light_color" 
				@clickItem="onChooseFun" />
		</view>
		<view class="lot_content">
			<scroll-view
				scroll-y="true" 
				:show-scrollbar="false"
				class="scroll-Y"
				@scrolltolower="lower">
				<LotCard :pageType="pageType" @chooseDeleteLotEmit="chooseDeleteLotFun" :isSelf="formData.byUser==='0' ? false : true" :lotData="item" v-for="(item,index) in allLots" :key="item.id" :itemIndex="index"/>
				<view class="load_box">
					<uni-load-more :content-text="contentText" iconType="circle" :color="primary_hot_color" :status="loadingStatus" />
				</view>
				<view v-if="!allLots.length">
					<MyNull img="nullEstate.png"/>
				</view>
			</scroll-view>
		</view>
		<MyModal :openModalFlag="modalEditLotFlag" :okFun="submitLotFun">
			<template #title>
				抓个阄
			</template>
			<template #content>
				<EditLot @changeEditLotEmit="changeEditLotEmitFun" :editLotFormData="editLotFormData" />
			</template>
		</MyModal>
		<MyModal :openModalFlag="modalIsDeleteLotFlag" :okFun="submitDeleteLotFun">
			<template #title>
				{{isDeleteTips}}
			</template>
		</MyModal>
		<view class="create_notice" @click="openCreateLot">
			<MyIcons size="130rpx" :src="joinSystemPath('addNotice.png')"/>
		</view>
	</view>
</template>

<script setup>
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import {ref} from 'vue'
	
	import {
		joinSystemPath,
		joinAvatarPath
	} from '@/utils/imgJoinName.js'
	import {
		verifyIsLogin,
		verifyFormData,
		verifyIsShowChooseAd
	} from '@/hooks/verify.js'
	import {
		getMonthDay,
	} from '@/utils/changeData.js'
	
	import LotCard from '@/components/Dormitory/LotCard/LotCard.vue'
	import Tips from '@/components/Tips/Tips.vue'
	import EditLot from '@/components/Dormitory/EditLot/EditLot.vue'
	import MyNull from '@/components/MyNull/MyNull.vue'
	
	import {dynamicStyleStore} from '@/stores/dynamicStyle.js'
	import {
		DormitoryModel
	} from '@/service/model/dormitory.js'
	
	const dynamicStyle_store = dynamicStyleStore()
	const dormitoryModel = new DormitoryModel()

	const {
		primary_hot_color,
		primary_light_color,
		primary_main_bgc,
		primary_shallow_color
	} = dynamicStyle_store.dormitoryStyle
	
	const dormitoryAllId=uni.getStorageSync('homeData')
	
	const formData=ref({
		limit:10,
		page:0,
		orderBy:'createAt.desc',
		byUser:'0'
	})
	
	const allLots=ref([])
	const loadingStatus = ref('more')
	const isDeleteTips = ref('')
	const contentText =ref({
		contentdown: '下滑加载更多',
		contentrefresh: '加载中',
		contentnomore: '没有更多了'
	})
	const isAllowGet=ref(true)
	const pageType = ref('')
	const init=()=>{
		formData.value.page=0
		allLots.value = []
		isAllowGet.value = true
	}
	//获取所有
	const getAllLots=async()=>{
		loadingStatus.value = 'loading'
		let data,success;
		switch(pageType.value){
			case 'lot':
				const {data:lotData,success:lotSuccess} = await dormitoryModel.getAllLots(formData.value,dormitoryAllId.dormitory_id)
				data = lotData
				success = lotSuccess
				break
			case 'randomLots':
				const {data:randomLotsData,success:randomLotsSuccess} = await dormitoryModel.getAllRandomLots(formData.value,dormitoryAllId.dormitory_id)
				data = randomLotsData
				success = randomLotsSuccess
				break
			case 'votes':
				const {data:votesData,success:voteSuccess} = await dormitoryModel.getAllVotes(formData.value,dormitoryAllId.dormitory_id)
				data = votesData
				success = voteSuccess
				break
		}
		if(success){
			allLots.value=[...allLots.value,...data]
			if(!data.length || data.length<formData.value.limit){
				isAllowGet.value = false
				loadingStatus.value = 'nomore'
			}else{
				loadingStatus.value = 'more'
			}
		}
	}
	
	const newLotId=ref('')
	
	//滚动到了底部
	const lower=()=>{
		if(!isAllowGet.value) return
		formData.value.page++
		getAllLots()
	}
	
	const items = ref([])
	//选择类型
	const onChooseFun=(e)=>{
		formData.value.byUser = e.currentIndex+''
		init ()
		getAllLots()
	}
	
	//编辑抓阄
	const editLotFormData=ref({
		title:'',
		part_in_nums:2,
		checked_nums:1
	})
	const editLotFormSchema={
		title:{
			required:true,
			tip:'活动标题'
		}
	}
	const modalEditLotFlag=ref(0)
	const openCreateLot=()=>{
		switch(pageType.value){
			case 'lot':
				modalEditLotFlag.value = Math.random()
				break
			case 'randomLots':
				uni.navigateTo({
					url:'/pages/Dormitory/CreateRandomLots/CreateRandomLots'
				})
				break
			case 'votes':
				uni.navigateTo({
					url:'/pages/Dormitory/CreateVote/CreateVote'
				})
				break
		}
	}
	const submitLotFun=async()=>{
		verifyIsLogin()
		verifyFormData(editLotFormData.value,editLotFormSchema)
		if(!editLotFormData.value.part_in_nums || !editLotFormData.value.checked_nums){
			uni.$emit('tips','人数必须大于0','warn')
			return
		}
		verifyIsShowChooseAd(getCurrentPages(),'创建抓阄')
		const {data,success} = await dormitoryModel.postAddLot(editLotFormData.value,dormitoryAllId.dormitory_id)
		if(success){
			uni.$emit('tips','创建成功','success')
			newLotId.value=data
			uni.navigateTo({
				url:`/pages/Dormitory/LotInfo/LotInfo?lot_id=${newLotId.value}&isNew=true`
			})
		}  
	}
	const changeEditLotEmitFun=(e)=>{
		editLotFormData.value = {...e}
	}
	
	//删除抓阄
	const modalIsDeleteLotFlag=ref(0)
	const chooseDeleteLot=ref({})
	const chooseDeleteLotFun=(e)=>{
		modalIsDeleteLotFlag.value=Math.random()
		chooseDeleteLot.value=e
	}
	const submitDeleteLotFun=async()=>{
		let success,data
		switch(pageType.value){
			case 'lot':
				const {data:lotData,success:lotSuccess} = await dormitoryModel.deleteLot({},chooseDeleteLot.value.id)
				success = lotSuccess
				data = lotData
				break
			case 'randomLots':
				const {data:randomLotsData,success:randomLotsDataSuccess} = await dormitoryModel.deleteRandomLots({},chooseDeleteLot.value.id)
				success = randomLotsDataSuccess
				data = randomLotsData;
				break
			case 'votes':
				const {data:voteData,success:voteSuccess} = await dormitoryModel.deleteVote({},chooseDeleteLot.value.id)
				success = voteSuccess
				data = voteData;
				break
		}
		if(success){
			const index=allLots.value.findIndex(item=>item.id===chooseDeleteLot.value.id)
			uni.$emit('tips',data,'success')
			allLots.value.splice(index,1)
		}
	}
	
	onShow(()=>{
		init()
		getAllLots()
	})
	
	onLoad((opt)=>{
		const {type:_type} = opt
		let title;
		pageType.value = _type
		switch(_type){
			case 'lot':
				items.value = ['所有抓阄','自己发起']
				isDeleteTips.value = '确认删除该抓阄吗'
				title='抓阄'
				break
			case 'randomLots':
				items.value = ['所有抽签','自己发起']
				isDeleteTips.value = '确认删除该抽签吗'
				title='抽签'
				break
			case 'votes':
				items.value = ['所有投票','自己发起']
				isDeleteTips.value = '确认删除该投票吗'
				title='投票'
				break
			default:
				uni.$emit('tips','页面错误','error')
		}
		uni.setNavigationBarTitle({
			title
		})
	})
	
</script>

<style lang="less">
	.text_ellipsis {
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
	.lots_box{
		background-color: v-bind(primary_main_bgc);
		.choose{
			padding: 20rpx 20rpx 0 20rpx;
			background-color: v-bind(primary_main_bgc);
			z-index: 2;
		}
		.lot_content{
			padding: 0 20rpx 40rpx 20rpx;
			background-color: v-bind(primary_main_bgc);
			.scroll-Y {
				height: calc(95vh);
				.load_box{
					margin-bottom: 3%;
				}
			}
		}
		.create_notice{
			width: 130rpx;
			height: 130rpx;
			position: fixed;
			bottom: 100rpx;
			right: 50rpx;
			z-index: 10;
			background-color: v-bind(primary_hot_color);
			border-radius: 100rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}
		// .create_notice:hover{
		// 	background-color: v-bind(primary_shallow_color);
		// }
	}
</style>
